<script setup lang="ts">
import { onMounted, ref } from 'vue';
import R1 from '@/assets/monitor/already/r1.svg'
import R2 from '@/assets/monitor/already/r2.svg'
import R3 from '@/assets/monitor/already/r3.svg'
import R4 from '@/assets/monitor/already/r4.svg'
import R5 from '@/assets/monitor/already/r5.svg'
import CTable from '@/components/table.vue'
import { incrementNumberComputed } from '../../utils';

// 数据格式
interface IData {
  // 名称
  title: string
  // 数量
  count: number
  // 占比
  percentage: number
  // 颜色
  color: string
}

interface IDataList {
  // 飞行任务
  task: string
  // /起降时间
  time: string
  // 起降场
  airport: string
}

const active = ref(0)
const handleClick = (data: number) => {
  active.value = data
}

const data = ref<IData[]>([])
const dataList = ref<IDataList[]>([])

const items = ref([
  {
    title: '起降时间',
    key: 'time',
  },
  {
    title: '起降场',
    key: 'airport'
  },
  {
    title: '飞行任务',
    key: 'task'
  },
])

onMounted(() => {
  // TODO: 接口回来 + active
  data.value = [
    {
      title: '短途运输',
      count: 102,
      percentage: 40.64,
      color: R1
    },
    {
      title: '航空护林',
      count: 72,
      percentage: 26.69,
      color: R2
    },
    {
      title: '空中巡查',
      count: 33,
      percentage: 13.15,
      color: R3
    },
    {
      title: '资源挂接率',
      count: 21,
      percentage: 8.37,
      color: R4
    },
    {
      title: '资源挂接率',
      count: 11,
      percentage: 4.38,
      color: R5
    }
  ].map((item) => {
    return {
      ...item,
      count: incrementNumberComputed(item.count, 1000),
      percentage: incrementNumberComputed(item.percentage, 1000),
    }
  }) as any

  dataList.value = [
    {
      task: '浏阳河巡检',
      time: '06-12 09:55:21-10:08:56',
      airport: '浏阳河起降场'
    },
    {
      task: '医疗物流',
      time: '06-12 09:55:21-10:08:56',
      airport: '梅溪湖起降场'
    },
    {
      task: '医疗物流',
      time: '06-12 09:55:21-10:08:56',
      airport: '梅溪湖起降场'
    },
    {
      task: '水域巡检',
      time: '06-12 09:55:21-10:08:56',
      airport: '梅溪湖起降场'
    },
    {
      task: '医疗物流',
      time: '06-12 09:55:21-10:08:56',
      airport: '梅溪湖起降场'
    }
  ]
})

// 点击表格行
const emit = defineEmits(['open-modal'])

const handleTableRow = (row: IDataList) => {
  emit('open-modal', 'yfrw')
  console.log(row)
}
</script>

<template>
  <div class="already">
    <div class="already-header"></div>
    <div class="already-content">
      <div class="already-content-tab card-tab">
        <div :class="{
          'already-content-tab-item card-tab-item': true,
          'already-content-tab-item-active card-tab-item-active': active === 0
        }" @click="handleClick(0)">
          热门飞行
        </div>
        <div :class="{
          'already-content-tab-item card-tab-item': true,
          'already-content-tab-item-active card-tab-item-active': active === 1
        }" @click="handleClick(1)">
          已飞列表
        </div>
      </div>
      <div v-if="active === 0" class="already-content-table">
        <div class="table-header">
          <div class="header-item">飞行任务</div>
          <div class="header-item">架次</div>
          <div class="header-item">占比</div>
        </div>
        <div class="table-body">
          <div class="table-row" v-for="(item, index) in data" :key="index">
            <div class="row-item task">
              <img class="task-icon" :src="item.color " />
              <div class="task-name">{{ item.title }}</div>
            </div>
            <div class="row-item count">{{ item.count }}</div>
            <div class="row-item percentage">{{ item.percentage }} <span>%</span></div>
            <div class="progress-basic"></div>
            <div class="progress" :style="{right: 100 - Number(item.percentage) + '%'}"></div>
          </div>
        </div>
      </div>
      <c-table v-else :items="items" :data="dataList" @click="handleTableRow"></c-table>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.already {
  width: 100%;
  display: flex;
  flex-direction: column;
  border-radius: 8px;
  background: linear-gradient(287deg, rgba(2, 53, 115, 0.6) 10%, rgba(2, 53, 115, 0.4) 39%, rgba(41, 162, 255, 0.2) 113%);
  backdrop-filter: blur(4px);

  &-header {
    width: 100%;
    height: 74px;
    margin-top: -38px;
    // margin-bottom: 6px;
    background-image: url('@/assets/monitor/already-header.svg');
    background-position: center;
    background-repeat: no-repeat;
    background-size: contain;
  }

  &-content {
    box-sizing: border-box;
    width: 100%;
    height: 270px;
    padding: 0px 20px 20px 20px;
  }
}
.already-content-table {
  margin-top: 10px;
  .table-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 30px;
    background: rgba(7, 47, 95, 0.3);
    padding: 0 20px;
    margin-bottom: 6px;

    .header-item {
      box-sizing: border-box;
      font-family: Source Han Sans;
      font-size: 14px;
      font-weight: bold;
      line-height: 30px;
      text-align: center;
      letter-spacing: normal;
      color: #FFFFFF;

      &:nth-child(1) {
        width: 40%;
        text-align: left;
        padding-left: 12px;
      }
      &:nth-child(2) {
        width: 30%;
      }
      &:nth-child(3) {
        width: 30%;
        text-align: right;
        padding-right: 12px;
      }
    }
  }
  .table-body {
    .table-row {
      position: relative;
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 24px;
      padding: 0;
      margin-bottom: 10px;

      &:last-child {
        margin: 0;
      }

      .row-item {
        font-family: Source Han Sans CN;
        font-size: 14px;
        font-weight: normal;
        line-height: 24px;
        letter-spacing: normal;
        color: #FFFFFF;

        &.task {
          width: 40%;
          display: flex;
          align-items: center;
          text-align: left;
          .task-icon {
            width: 24px;
            height: 24px;
            border-radius: 50%;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 14px;
            font-weight: bold;
            margin-right: 10px;
          }
          .task-name {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
          }
        }
        &.count {
          width: 30%;
          font-family: Source Han Sans CN;
          font-size: 14px;
          font-weight: 500;
          line-height: 20.08px;
          text-align: center;
          letter-spacing: normal;
          /* 字体颜色-正文色 */
          color: #FFFFFF;
        }
        &.percentage {
          width: 30%;
          text-align: right;
          font-family: YouSheBiaoTiHei;
          font-size: 20px;
          font-weight: normal;
          line-height: 24px;
          letter-spacing: normal;
          /* 文字颜色/主标题&一级标题 */
          color: #FFFFFF;

          span {
            font-family: Microsoft YaHei;
            font-size: 14px;
            font-weight: normal;
            line-height: 20px;
            letter-spacing: normal;
            color: #A3B0CC;
          }
        }
      }

      .progress-basic {
        position: absolute;
        bottom: -4px;
        left: 32px;
        right: 0;
        height: 4px;
        background: rgba(7, 47, 95, 0.3);
      }

      .progress {
        position: absolute;
        bottom: -4px;
        left: 32px;
        height: 4px;
        background: linear-gradient(270deg, #188EE7 6%, #1695F6 59%, rgba(21, 153, 253, 0) 100%);
        box-sizing: border-box;
        /* 2 中性色/白色(color-bg-white) */
        border-width: 0px 6px 0px 0px;
        border-style: solid;
        border-color: #FFFFFF;
        transition: all 0.5s ease-out;
      }
    }
  }
}
</style>
